# setwd("~/Dropbox/T&G project survey/Ukraine/replication_JOP")
library(tidyverse)
library(scales)
library(labelled)
library(countrycode)
library(haven)
source("func.R")

# ggplot defaults: geoms theme
update_geom_defaults("text", list(family = "Archivo Narrow"))
update_geom_defaults("label", list(family = "Archivo Narrow"))
theme_set(theme_nice())

eb = read_dta("data/Flash_EuroBaro506/ZA7871_v1-0-0.dta") %>%
  # filter(!isocntry %in% c("MT", "CY", "LU")) %>%
  mutate(
    country = countrycode(isocntry, "iso2c", "country.name"),
    q1_1 = to_factor(q1_1),
    q1_2 = to_factor(q1_2),
    q2_1 = to_factor(na_if(q2_1, 998)),
    q2_2 = to_factor(na_if(q2_2, 998)),
    q2_3 = to_factor(na_if(q2_3, 998)),
    q2_5 = to_factor(na_if(q2_5, 998)),
    q4_1 = to_factor(na_if(q4_1, 998)),
    q4_2 = to_factor(na_if(q4_2, 998)),
    q4_3 = to_factor(na_if(q4_3, 998)),
    q4_4 = to_factor(na_if(q4_4, 998))) %>%
  select(country,
    `How often follow news on Ukraine` = q1_1,
    `How often discuss Ukraine with friends` = q1_2,
    `Satisfied with own country response` = q2_1,
    `Satisfied with EU response` = q2_2,
    `Satisfied with NATO response` = q2_3,
    `Satisfied with US response` = q2_5,
    `Sanctions against Russia` = q4_1,
    `Sanctions against oligarchs` = q4_2,
    `Financial support to Ukraine` = q4_3,
    `Military support to Ukraine` = q4_4) %>%
  pivot_longer(-country) %>%
  filter(!is.na(value)) %>%
  group_by(country, name, value) %>%
  tally() %>%
  mutate(pct = n/sum(n)) %>%
  # mutate(country = ifelse(country == "Spain", "SPAIN", country)) %>%
  # mutate(country = to_upper(country)) %>%
  mutate(name2 = recode(name,
    "How often follow news on Ukraine" = "q1_1",
    "How often discuss Ukraine with friends" = "q1_2",
    "Satisfied with own country response" = "q2_1",
    "Satisfied with EU response" = "q2_2",
    "Satisfied with NATO response" = "q2_3",
    "Satisfied with US response" = "q2_5",
    "Sanctions against Russia" = "q4_1",
    "Sanctions against oligarchs" = "q4_2",
    "Financial support to Ukraine" = "q4_3",
    "Military support to Ukraine" = "q4_4")) %>%
  mutate(psize = ifelse(tolower(country) == "spain", 2, 1))

# Plots
aware = c("q1_1", "q1_2")
resp = c("q2_1", "q2_2", "q2_3", "q2_5")
approve = c("q4_1", "q4_2", "q4_3", "q4_4")

colors = rep("#00000080", length(unique(eb$country)))
colors[which(unique(eb$country) == "Spain")] = "#F8766D"

# Awareness
p = ggplot(eb %>% filter(name2 %in% aware), aes(x = value, y = pct, color = country)) +
  geom_point(aes(size = psize)) +
  facet_wrap(vars(name), ncol = 4) +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels = label_wrap(10)) +
  labs(x = "", y = "", subtitle = "(Spain in red)") +
  scale_color_manual(values = colors) +
  theme(legend.position = "none",
    plot.subtitle = element_text(size = 8, color = "#F8766D")) +
  scale_size(range = c(1, 2))
ggsave("figures/EB_awareness.pdf", height = 3.25, width = 5, device = cairo_pdf)

# Agree with responses
p = ggplot(eb %>% filter(name2 %in% resp), aes(x = value, y = pct, color = country)) +
  geom_point(aes(size = psize)) +
  facet_wrap(vars(name), ncol = 2) +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels = label_wrap(8)) +
  labs(x = "", y = "", subtitle = "(Spain in red)") +
  scale_color_manual(values = colors) +
  theme(legend.position = "none",
    plot.subtitle = element_text(size = 8, color = "#F8766D")) +
  scale_size(range = c(1, 2))
ggsave("figures/EB_responses.pdf", height = 5, width = 5, device = cairo_pdf)

# Agree with responses
p = ggplot(eb %>% filter(name2 %in% approve), aes(x = value, y = pct, color = country)) +
  geom_point(aes(size = psize)) +
  facet_wrap(vars(name), ncol = 2) +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels = label_wrap(10)) +
  labs(x = "", y = "", subtitle = "(Spain in red)") +
  scale_color_manual(values = colors) +
  theme(legend.position = "none",
    plot.subtitle = element_text(size = 8, color = "#F8766D")) +
  scale_size(range = c(1, 2))
ggsave("figures/EB_measures.pdf", height = 5, width = 5, device = cairo_pdf)
